package com.example.tanloadpic.utils;

import android.os.Looper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CommonUtils {


    public static boolean isMain(){
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }

    public static boolean checkMainThread(){
        if (!isMain()){
            throw new IllegalThreadStateException("請在主綫程裏面執行,兄弟");
        }
        return false;
    }



    /**
     * md5 工具类
     * @param string    你要转化的东东
     * @return 转化后的MD5
     */
    public static String stringToMd5(String string) {
        String cacheKey;
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(string.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(string.hashCode());
        }
        return cacheKey;
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }


}
